package com.bootdo.common.service.impl;

import com.bootdo.common.dao.FileAppsDao;
import com.bootdo.common.domain.FileAppsDO;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileAppsService;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.ShiroUtils;
import com.smart.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;



@Service
public class FileAppsServiceImpl implements FileAppsService {
	@Autowired
	private FileAppsDao fileAppsDao;
	@Autowired
	private FileService fileService;
	
	@Override
	public FileAppsDO get(Integer id){
		return fileAppsDao.get(id);
	}
	
	@Override
	public List<FileAppsDO> list(Map<String, Object> map){
		return fileAppsDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return fileAppsDao.count(map);
	}
	
	@Override
	public int save(FileAppsDO fileApps){
		return fileAppsDao.save(fileApps);
	}
	
	@Override
	public int update(FileAppsDO fileApps){
		return fileAppsDao.update(fileApps);
	}
	
	@Override
	public int remove(Integer id){
		return fileAppsDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return fileAppsDao.batchRemove(ids);
	}
	@Override
	public int saveFormFiles(Integer formId,String formName,String fileIds) {
		if(!StringUtils.isEmpty(fileIds)) {
			//创建1个中间表 表单名称 表单id 文件id
			String[] ids = fileIds.split(",");
			for(String fileId : ids) {
				if(StringUtils.hasContent(fileId)) {
					saveFormFile(formId,formName,fileId);
				}
			}
			
		}
		return 0;
	}

	@Override
	public int saveFormFile(Integer formId, String formName, String fileId) {
		
		Integer fid = Integer.parseInt(fileId);
		
		FileAppsDO fileAppsDO = new FileAppsDO();
		
		fileAppsDO.setFileId(fid);
		fileAppsDO.setFormId(formId);
		fileAppsDO.setFormName(formName);
		fileAppsDO.setCreateTime(new Date());
		fileAppsDO.setStatus(1);
		fileAppsDO.setCreateBy(ShiroUtils.getUser().getUsername());
		
		if(save(fileAppsDO)>0) {
			//保存成功后更新文件状态
			FileDO fileDO = fileService.get(Long.parseLong(fileId));
			if(fileDO!=null) {
				fileDO.setStatus(1);
				fileService.update(fileDO);
			}
			
		}
		
		return 0;
	}

	@Override
	public List<FileDO> getFormFiles(Integer formId, String formName) {
		List<FileDO> list = fileService.getFormFiles(formId,formName);
		return list;
	}

	@Override
	public int removeBySysFileId(Long id) {
		return fileAppsDao.removeBySysFileId(id);
	}
	
}
